<?php

require_once 'components/input_values.php';
require_once 'components/utils/string_utils.php';

class FilterOperator
{
    /**
     * @var FilterOperator
     */
    public static $Equlas;
    
    /**
     * @var FilterOperator[]
     */
    private static $operators;

    public static function Init()
    {
        FilterOperator::$Equlas = new FilterOperator('=');
        
        self::$operators = array(
            FilterOperator::$Equlas
        );
    }

    /**
     * @param string $shortName
     * @return FilterOperator
     */
    public static function GetOperatorFromShortName($shortName)
    {
        foreach(self::$operators as $operator)
        {
            if ($operator->GetShortName() == $shortName)
                return $operator;
        }
        return null;
    }

    /**
     * @var string
     */
    private $shortName;

    /**
     * @param string $shortName
     */
    public function __construct($shortName)
    {
        $this->shortName = $shortName;
    }

    public function GetShortName()
    {
        return $this->shortName;
    }
}
FilterOperator::Init();


class DataFilter
{
    /**
     * @var string
     */
    private $fieldName;

    /**
     * @var FilterOperator
     */
    private $operator;

    /**
     * @var mixed
     */
    private $value;

    public function __construct($fieldName, $operator, $value)
    {
        $this->fieldName = $fieldName;
        $this->operator = $operator;
        $this->value = $value;
    }

    /**
     * @return string
     */
    public function GetFieldName()
    {
        return $this->fieldName;
    }

    /**
     * @return FilterOperator
     */
    public function GetOperator()
    {
        return $this->operator;
    }

    /**
     * @return mixed
     */
    public function GetValue()
    {
        return $this->value;
    }
}

class DataFilters
{
    /**
     * @var DataFilter[]
     */
    private $filters;
    
    const FieldNameParam = 'filter%dfield';
    const ValueParam = 'filter%dvalue';
    const OperatorParam = 'filter%doperator';


    public function __construct()
    {
        $this->filters = array();
    }

    /**
     * @param IInputValues $values
     * @param int $paramIndex
     * @return bool
     */
    private function HasFilterParams(IInputValues $values, $paramIndex)
    {
        return
            $values->HasValue(StringUtils::Format(self::FieldNameParam, $paramIndex)) &&
            $values->HasValue(StringUtils::Format(self::ValueParam, $paramIndex)) &&
            $values->HasValue(StringUtils::Format(self::OperatorParam, $paramIndex));
    }

    /**
     * @param IInputValues $values
     * @return void
     */
    public function ExtractFrom(IInputValues $values)
    {
        $paramIndex = 0;
        while($this->HasFilterParams($values, $paramIndex))
        {
            $this->AddFilter(new DataFilter(
                $values->GetValue(StringUtils::Format(self::FieldNameParam, $paramIndex)),
                FilterOperator::GetOperatorFromShortName(
                    $values->GetValue(StringUtils::Format(self::OperatorParam, $paramIndex))
                ),
                $values->GetValue(StringUtils::Format(self::ValueParam, $paramIndex))
            ));
            $paramIndex++;
        }
    }

    /**
     * @param DataFilter $dataFilter
     * @return void
     */
    public function AddFilter(DataFilter $dataFilter)
    {
        $this->filters[] = $dataFilter;
    }

    /**
     * @return int
     */
    public function GetCount()
    {
        return count($this->filters);
    }

    /**
     * @param int $index
     * @return DataFilter
     */
    public function GetFilter($index)
    {
        return $this->filters[$index];
    }
}

?>